/*
 * HSM Proxy Project.
 * Copyright (C) 2013 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see 
 * http://www.gnu.org/licenses/.
 */

package be.fedict.hsm.entity;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Query;
import javax.persistence.Table;

@Entity
@Table(name = Constants.TABLE_NAME_PREFIX + "application_key")
@NamedQueries({
		@NamedQuery(name = ApplicationKeyEntity.QUERY_APP, query = "SELECT ak FROM ApplicationKeyEntity AS ak "
				+ "WHERE ak.application = :application"),
		@NamedQuery(name = ApplicationKeyEntity.QUERY_KEYSTORE, query = "SELECT DISTINCT(ak.application) FROM ApplicationKeyEntity AS ak "
				+ "WHERE ak.keyStore = :keyStore") })
public class ApplicationKeyEntity implements Serializable {

	private static final long serialVersionUID = 1L;

	public static final String QUERY_APP = "ake.app";

	public static final String QUERY_KEYSTORE = "ake.keystore";

	@EmbeddedId
	private ApplicationKeyId id;

	@MapsId("applicationId")
	@ManyToOne
	private ApplicationEntity application;

	@Column(name = "name", insertable = false, updatable = false)
	private String name;

	@ManyToOne
	private KeyStoreEntity keyStore;

	private String keyStoreKeyAlias;

	public ApplicationKeyEntity() {
		super();
	}

	public ApplicationKeyEntity(ApplicationEntity application, String name) {
		this.id = new ApplicationKeyId(application.getId(), name);
		this.name = name;
		this.application = application;
	}

	public ApplicationKeyId getId() {
		return this.id;
	}

	public ApplicationEntity getApplication() {
		return this.application;
	}

	public String getName() {
		return this.name;
	}

	public static List<ApplicationKeyEntity> getApplicationKeys(
			EntityManager entityManager, ApplicationEntity application) {
		Query query = entityManager.createNamedQuery(QUERY_APP);
		query.setParameter("application", application);
		return query.getResultList();
	}

	public static List<ApplicationEntity> getApplications(
			EntityManager entityManager, KeyStoreEntity keyStoreEntity) {
		Query query = entityManager.createNamedQuery(QUERY_KEYSTORE);
		query.setParameter("keyStore", keyStoreEntity);
		return query.getResultList();
	}

	public KeyStoreEntity getKeyStore() {
		return this.keyStore;
	}

	public void setKeyStore(KeyStoreEntity keyStore) {
		this.keyStore = keyStore;
	}

	public String getKeyStoreKeyAlias() {
		return this.keyStoreKeyAlias;
	}

	public void setKeyStoreKeyAlias(String keyStoreKeyAlias) {
		this.keyStoreKeyAlias = keyStoreKeyAlias;
	}
}
